import { dataApi } from '@/constants/api/data-api';
import { useNotice } from '@/components/element-plus/notice';
import { Options, ResponseCodeEnum, useHttpClient } from 'pf-component';
import { formatDate } from '@/constants/util/date-util';

export const useCourse = () => {
  const { message } = useNotice();
  const listByCurrentDay = (params: any = {}): Promise<any[]> => {
    const day = new Date();
    params.start = formatDate(
      'yyyy-MM-dd hh:mm:ss',
      new Date(day.getFullYear(), day.getMonth(), day.getDate(), 0, 0, 0)
    );
    params.end = formatDate(
      'yyyy-MM-dd hh:mm:ss',
      new Date(day.getFullYear(), day.getMonth(), day.getDate(), 23, 59, 59)
    );
    return list(params);
  };
  const list = (params: any = {}): Promise<any[]> => {
    return useHttpClient()
      .general<any[]>(dataApi.courseApi.list, undefined, params)
      .then(res => {
        if (res.code === ResponseCodeEnum.SUCCESS) {
          return res.data;
        } else {
          message.error(res.message);
          return [];
        }
      });
  };
  const preview = (query: { start: Date; end: Date }): Promise<any[]> => {
    return useHttpClient()
      .general<any[]>(dataApi.courseApi.preview, {
        start: formatDate('yyyy-MM-dd', query.start),
        end: formatDate('yyyy-MM-dd', query.end)
      })
      .then(res => {
        if (res.code === ResponseCodeEnum.SUCCESS) {
          return res.data;
        } else {
          message.error(res.message);
          return [];
        }
      });
  };
  const update = (body: any): Promise<boolean> => {
    return useHttpClient()
      .general(dataApi.courseApi.addOrUpd, { classId: body.classId }, body)
      .then(res => {
        if (res.code !== ResponseCodeEnum.SUCCESS) {
          message.error(res.message);
        }
        return res.code === ResponseCodeEnum.SUCCESS;
      });
  };
  const del = (body: string[]): Promise<boolean> => {
    return null as any;
  };
  return { list, listByCurrentDay, update, del, preview };
};
